<?php

namespace Common\Lib\Payments;

/**
 * @class Payment
 * @brief 支付方式 操作类
 */
class Payment {

    /**
     * @brief 创建支付类实例
     * @param $payment_id int 支付方式ID
     * @return \Common\Lib\Payments\PaymentPlugin;
     */
    public static function createPaymentInstance($payment_id, $backParam) {
        $payment = self::getPaymentById($payment_id);

        if ($payment && isset($payment['class_name']) && $payment['class_name']) {
            $classPath = __DIR__ . "/{$payment['class_name']}/{$payment['class_name']}.class.php";
            $className = '\\' . __NAMESPACE__ . "\\{$payment['class_name']}\\{$payment['class_name']}";
            if (file_exists($classPath)) {
                return new $className($payment, $backParam);
            } else {
                throw new Exception("支付接口类【{$payment['class_name']}】没有找到");
            }
        } else {
            throw new Exception("支付方式不存在【{$payment_id}】");
        }
    }

    /**
     * @brief 根据支付方式配置编号  获取该插件的详细配置信息
     * @param $payment_id int    支付方式ID
     * @param $key        string 字段
     * @return 返回支付插件类对象
     */
    public static function getPaymentById($payment_id, $key = '') {
        $payment = D("Payment")->find($payment_id);

        if ($key) {
            return isset($payment[$key]) ? $payment[$key] : '';
        }
        return $payment;
    }

    /**
     * @brief 根据支付方式配置编号 获取该插件的配置信息
     * @param $payment_id int 支付方式ID
     * @param $key string 字段
     * @return 返回支付插件类对象
     */
    public static function getConfigParam($payment_id, $key = '') {
        $payConfig = self::getPaymentById($payment_id, 'config_param');
        if ($payConfig) {
            $payConfig = json_decode($payConfig, true);
            return isset($payConfig[$key]) ? $payConfig[$key] : '';
        }
        return '';
    }

    /**
     * @brief 获取订单中的支付信息 M:必要信息; R表示店铺; P表示用户;
     * @param $payment_id int 支付方式ID
     * @param $type string 信息获取方式 order:订单支付;recharge:在线充值;
     * @param $argument mix 参数
     * @return array 支付提交信息
     */
    public static function getPaymentInfo($payment_id, $argument) {
        //最终返回值
        $payment = array();

        //初始化配置参数
        $paymentInstance = Payment::createPaymentInstance($payment_id);
        $configParam = $paymentInstance->configParam();
        foreach ($configParam as $key => $val) {
            $payment[$key] = '';
        }

        //获取公共信息
        $paymentRow = self::getPaymentById($payment_id, 'config_param');
        if ($paymentRow) {
            $paymentRow = json_decode($paymentRow, true);
            foreach ($paymentRow as $key => $item) {
                $payment[$key] = $item;
            }
        }

        //获取订单信息
        if (empty($argument)) {
            throw new Exception('订单不存在');
        }
        if ($argument['status'] == 1) {
            throw new Exception('订单状态不正确，不能进行支付');
        }
        $payment['R_Name'] = $argument['remark'];
        $payment['M_Remark'] = $argument['remark'];
        $payment['M_OrderId'] = $argument['id'];
        $payment['M_OrderNO'] = $argument['order_id'];
        $payment['M_Amount'] = $argument['amount'];
        $payment['M_PayId'] = $argument['pay_id'];
        //用户信息
        $payment['P_UserId'] = $argument['user_id'];
        $payment['P_Name'] = $argument['accept_name'];
        $payment['P_PostCode'] = $argument['postcode'];
        $payment['P_Phone'] = $argument['phone'];
        $payment['P_Address'] = $argument['address'];


        //交易信息
        $payment['M_Time'] = NOW_TIME;
        $payment['M_Paymentid'] = $payment_id;

        return $payment;
    }

}